<<<<<<< HEAD
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 用户表的列表信息
 * @author chenhuan
 *
 */
class Userborrow extends MY_Controller {
	
	function __construct(){
		parent::__construct();
		/** 登陆验证 */
		$user = $this->session->userdata('rsh_user');
		if(!isset($user['user_id'])){
			$str = $_SERVER['REQUEST_URI'];
			$this->js->go('/login.html');
		}elseif(!$user['user_id']){
			$this->js->go('/login.html');
		}
		
		$this->tab = 'user';
		$this->sub = 'borrow';
		
	}
	
	
	/**
	 * 成功投资的借款标
	 */
	function zhs_success(){
		$per_page 			= $this->input->get('per_page')?$this->input->get('per_page'):0;
		$user = $this->session->userdata('rsh_user');
		$data['where']['user_id'] 	= $user['user_id'];
		$data['where']['fstatus'] 	= 1;
		$data['page_size']  = USER_CENTER_PAGE_SIZE;
		$data['page_start'] = (int)$per_page;
		
		$this->load->model('Borrow_model');
		$vdata = $this->get_user_info('tender_success','success','tender');
		$tender_vdata= $this->Borrow_model->borrow_tender_user_list($data);

		//投标实际总金额
		$sj_account = 0;
		if( !empty($tender_vdata['tender_list']) ){
			foreach( $tender_vdata['tender_list'] as $k => $v ){
				$sj_account += $v['account'];
				if($v['account'] <= $v['account_yes']) $v['fstatus'] = 3;
			}
		}
		$vdata['tender_money_count'] = $sj_account;
		$vdata['tender_list'] = $tender_vdata['tender_list'];//成功投资的列表
		//分页
		$this->load->library('pagination');
		$config['base_url'] 	= "/userborrow/success.html?";
		$config['total_rows'] 	= $vdata['tender_count'];
		$config['per_page'] 	= USER_CENTER_PAGE_SIZE; 
		$this->pagination->initialize($config); 
		
		$vdata['type'] = 1;
		$vdata['title'] = "成功投资的借款标".' - '.CON_KEYWORDS; 
		$this->load->view('/user/zhs_user_tender_success', $vdata);
		
	}
	
	
	/**
	 * 待收明细
	 */
	function zhs_coll(){
		$act = $this->input->get('act');
		if ($act == 'excel'){
			$this->collexcel();
		}else{
			$per_page	= $this->input->get('per_page')?$this->input->get('per_page'):0;
			$user = $this->session->userdata('rsh_user');
			
			$data['where']['user_id'] 	= $user['user_id'];
			$data['where']['fstatus'] 	= 0;
			$data['where']['strtime'] 	= $this->input->get('strtime') ? $this->input->get('strtime') : '';
			$data['where']['endtime'] 	= $this->input->get('endtime') ? $this->input->get('endtime') : '';
			$data['select'] 	= 1;
			$data['order'] 		= 1;
			$data['page_size']  = USER_CENTER_PAGE_SIZE;
			$data['page_start'] = (int)$per_page;
			$this->load->model('Borrow_model');
		
			$vdata = $this->get_user_info('tender_coll','success','tender');
			$data['type'] = 3;
			$coll_vdata = $this->Borrow_model->get_user_collection($data);
			$vdata['collection_list'] = $coll_vdata['collection_list'];
			//if(count($vdata['collection_list']) < 1)$this->js->alert('查询无符合条件的数据');
			//分页
			$this->load->library('pagination');
			$config['base_url'] = "/userborrow/zhs_coll.html?strtime=".$data['where']['strtime']."&endtime=".$data['where']['endtime'];
			$config['total_rows'] = $vdata['collection_count'];
			$config['per_page'] = USER_CENTER_PAGE_SIZE; 
			$this->pagination->initialize($config); 
			$vdata['type'] = $data['type'];
			$vdata['title']	= "未收款明细".' - '.CON_KEYWORDS;
			$vdata['sub'] = 'tender';
			$vdata['user_sub'] ='tender_colldet';
			$vdata['where']['strtime'] 	= $this->input->get('strtime') ? $this->input->get('strtime') : '';
			$vdata['where']['endtime'] 	= $this->input->get('endtime') ? $this->input->get('endtime') : '';
			$vdata['sub_1'] = "会员中心";
			$vdata['sub_1_href'] = "/user/zhs_center.html";
			$vdata['sub_2'] = "待收回款";
			$vdata['sub_2_href'] = "/userborrow/zhs_coll.html";
			$vdata['borrow_type'] 	= $this->Borrow_model->arrays('borrow_type');//标类型
			//var_dump($vdata);exit;
			$this->load->view('/user/zhs_user_borrow_ycolldetail', $vdata);
		}
	}
	
	function collexcel(){
		$user = $this->session->userdata('rsh_user');
		$data['where']['user_id'] 	= $user['user_id'];
		$data['where']['fstatus'] 	= 0;
		$data['where']['strtime'] 	= $this->input->get('strtime') ? $this->input->get('strtime') : '';
		$data['where']['endtime'] 	= $this->input->get('endtime') ? $this->input->get('endtime') : '';
		$data['select'] 	= 1;
		$data['order'] 		= 1;
		$data['limit'] = 'all';
		$this->load->model('Borrow_model');
		$list = $this->Borrow_model->get_user_collection($data);
		
		$this->load->library('PHPExcel');
		$this->load->library('PHPExcel/IOFactory'); 
		$objPHPExcel = new PHPExcel();
		$objPHPExcel->getProperties()->setTitle("待收明细")->setDescription(COMPANY_NAME); 
		$objPHPExcel->setActiveSheetIndex(0);
		$titles = array("借款标", "期数", "应收时间", "应收本金", "应收利息");
		
		for($i = 0; $i < count($titles); $i ++){
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($i, 1, $titles[$i]);
		}
		
		for($i = 0; $i < count($list); $i++){
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(0, $i+2, $list[$i]['title']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(1, $i+2, $list[$i]['time_limit']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(2, $i+2, date('Y-m-d',$list[$i]['repay_time']));
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(3, $i+2, $list[$i]['capital']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(4, $i+2, $list[$i]['interest']);
		}
		
		$objWriter = IOFactory::createWriter($objPHPExcel, 'Excel5'); 
		//发送标题强制用户下载文件 
		header('Content-Type: application/vnd.ms-excel'); 
		header('Content-Disposition: attachment;filename="待收明细.xls"'); 
		header('Cache-Control: max-age=0'); 
		$objWriter->save('php://output'); 
	}
	
	
	/**
	 * 已收明细
	 */
	function zhs_ycoll(){
		$per_page 			= $this->input->get('per_page')?$this->input->get('per_page'):0;
		
		$user = $this->session->userdata('rsh_user');
		$data['where']['strtime'] 	= $this->input->get('strtime') ? $this->input->get('strtime') : '';
		$data['where']['endtime'] 	= $this->input->get('endtime') ? $this->input->get('endtime') : '';
		$data['where']['user_id'] 	= $user['user_id'];
		$data['where']['fstatus'] 	= 1;
		$data['select'] 	= 1;
		$data['order'] 		= 4;
		$data['page_size']  = USER_CENTER_PAGE_SIZE;
		$data['page_start'] = (int)$per_page;
		$this->load->model('Borrow_model');
		$vdata = $this->get_user_info('tender_ycoll','success','tender');
		
		$data['type'] = 4;
		$ycoll_vdata = $this->Borrow_model->get_user_collection($data);
		$vdata['collection_list'] = $ycoll_vdata['collection_list'];
		//if(count($vdata['collection_list']) < 1)$this->js->alert('查询无符合条件的数据');
		//分页
		$this->load->library('pagination');
		$config['base_url'] = "/userborrow/zhs_ycoll.html?";
		$config['total_rows'] = $vdata['collection_count'];
		$config['per_page'] = USER_CENTER_PAGE_SIZE; 
		$this->pagination->initialize($config); 
		$vdata['where']['strtime'] 	= $this->input->get('strtime') ? $this->input->get('strtime') : '';
		$vdata['where']['endtime'] 	= $this->input->get('endtime') ? $this->input->get('endtime') : '';
		$vdata['type'] = $data['type'];
		$vdata['sub'] = 'tender';
		$vdata['user_sub'] ='tender_colldet';
		$vdata['title'] 						= "已收明细".' - '.CON_KEYWORDS;
		$vdata['sub_1'] = "会员中心";
		$vdata['sub_1_href'] = "/user/zhs_center.html";
		$vdata['sub_2'] = "已收回款";
		$vdata['sub_2_href'] = "/userborrow/zhs_ycoll.html";
		$vdata['borrow_type']	= $this->Borrow_model->arrays('borrow_type');//标类型
		$this->load->view('/user/zhs_user_borrow_ycolldetail', $vdata);
	}
	
	//******************************
	function zsh_ycoll(){//投资
		$per_page 		= $this->input->get('per_page')?$this->input->get('per_page'):0;
		
		$user = $this->session->userdata('rsh_user');
		$data['where']['user_id'] 	= $user['user_id'];
		$data['where']['fstatus'] 	= 1;
		$data['select'] 	= 1;
		$data['order'] 		= 4;
		$data['page_size']  = USER_CENTER_PAGE_SIZE;
		$data['page_start'] = (int)$per_page;
		$this->load->model('Borrow_model');
		$vdata = $this->get_user_info('tender_ycoll','success','tender');
		
		$ycoll_vdata = $this->Borrow_model->get_user_collection($data);
		$vdata['collection_list'] = $ycoll_vdata['collection_list'];
		
		//分页
		$this->load->library('pagination');
		$config['base_url'] = "/userborrow/ycoll.html?";
		$config['total_rows'] = $vdata['collection_count'];
		$config['per_page'] = USER_CENTER_PAGE_SIZE; 
		$this->pagination->initialize($config); 
		$vdata['type'] = 4;
		$vdata['title'] 						= "投资赎回".' - '.CON_KEYWORDS;
		$vdata['user_sub'] 						= "tender_coll";
		$vdata['sub'] 						= "tender";
		$this->load->view('/user/zhs_user_borrow_ycoll', $vdata);
	}
	
	function zsh_ycoll_detail(){//回款投资：包括未回款、已回款
		$per_page 		= $this->input->get('per_page')?$this->input->get('per_page'):0;
		$user = $this->session->userdata('rsh_user');
		$data['where']['user_id'] 	= $user['user_id'];
		$data['where']['fstatus'] 	= 1;
		$data['select'] 	= 1;
		$data['order'] 		= 4;
		$data['page_size']  = USER_CENTER_PAGE_SIZE;
		$data['page_start'] = (int)$per_page;
		$this->load->model('Borrow_model');
		$vdata = $this->get_user_info('tender_ycoll','success','tender');
		
		$ycoll_vdata = $this->Borrow_model->get_user_collection($data);
		$vdata['collection_list'] = $ycoll_vdata['collection_list'];
		
		//分页
		$this->load->library('pagination');
		$config['base_url'] = "/userborrow/ycoll.html?";
		$config['total_rows'] = $vdata['collection_count'];
		$config['per_page'] = USER_CENTER_PAGE_SIZE; 
		$this->pagination->initialize($config); 
		$vdata['type'] = 4;
		$vdata['title'] 						= "回款明细".' - '.CON_KEYWORDS;
		$vdata['user_sub'] 						= "tender_colldet";
		$vdata['sub'] 						= "tender";
		$this->load->view('/user/zhs_user_borrow_ycolldetail', $vdata);
	}
	

	/**
	 * 正在投资的借款标
	 */
	function now(){
		$per_page 			= $this->input->get('per_page')?$this->input->get('per_page'):0;
		
		$user = $this->session->userdata('rsh_user');
		$data['where']['user_id'] 	= $user['user_id'];
		$data['where']['fstatus'] 	= 1;
		$data['select'] 			= 1;
		$data['page_size']  		= USER_CENTER_PAGE_SIZE;
		$data['page_start'] 		= (int)$per_page;
		$this->load->model('Borrow_model');
		
		$vdata = $this->get_user_info('tender_now','success','tender');
		
		$now_vdata = $this->Borrow_model->get_borrow_tender_lists($data);
		$vdata['tender_list'] = $now_vdata['tender_list'];
		
		//分页
		$this->load->library('pagination');
		$config['base_url'] = "/userborrow/now/index.html?";
		$config['total_rows'] = $vdata['tender_count'];
		$config['per_page'] = USER_CENTER_PAGE_SIZE; 
		$this->pagination->initialize($config); 
		$vdata['type'] = 2;
		$vdata['title'] 						= "正在投标的借款".' - '.CON_KEYWORDS;
		$this->load->view('/user/user_borrow_now', $vdata);
	}
	
	/**
	 * 正在投资的借款标
	 */
	function zhs_now(){
		$per_page 			= $this->input->get('per_page')?$this->input->get('per_page'):0;
		
		$user = $this->session->userdata('rsh_user');
		$data['where']['user_id'] 	= $user['user_id'];
		$data['where']['fstatus'] 	= 1;
		$data['select'] 			= 1;
		$data['page_size']  		= USER_CENTER_PAGE_SIZE;
		$data['page_start'] 		= (int)$per_page;
		$this->load->model('Borrow_model');
		
		$vdata = $this->get_user_info('tender_now','success','tender');
		
		$now_vdata = $this->Borrow_model->get_borrow_tender_lists($data);
		$vdata['tender_list'] = $now_vdata['tender_list'];
		
		//分页
		$this->load->library('pagination');
		$config['base_url'] = "/userborrow/now/index.html?";
		$config['total_rows'] = $vdata['tender_count'];
		$config['per_page'] = USER_CENTER_PAGE_SIZE; 
		$this->pagination->initialize($config); 
		$vdata['type'] = 2;
		$vdata['title'] 						= "正在投标的借款".' - '.CON_KEYWORDS;
		$this->load->view('/user/zhs_user_borrow_now', $vdata);
	}
	
	function publish(){
		$per_page 			= $this->input->get('per_page')?$this->input->get('per_page'):0;
		
		$user = $this->session->userdata('rsh_user');
		$data['where']['user_id'] 	= $user['user_id'];
		$data['where']['fstatus'] 	= 1;
		$data['select'] 	= 1;
		$data['page_size']  = USER_CENTER_PAGE_SIZE;
		$data['page_start'] = (int)$per_page;
		$this->load->model('Borrow_model');
		$vdata = $this->get_user_info('borrow_publish','publish');
		
		$borrow_vdata = $this->Borrow_model->get_user_borrow($data);
		$vdata['borrow_list'] = $borrow_vdata['borrow_list'];
		
		//分页
		$this->load->library('pagination');
		$config['base_url'] = "/userborrow/publish/index.html?";
		$config['total_rows'] = $vdata['borrow_count'];
		$config['per_page'] = USER_CENTER_PAGE_SIZE; 
		$this->pagination->initialize($config); 
		$vdata['borrow_type'] 	= $this->Borrow_model->arrays('borrow_type');//标种
		$vdata['type'] = 1;
		$vdata['title'] 						= "正在招标的借款".' - '.CON_KEYWORDS;
		$this->load->view('/user/user_loan_borrow', $vdata);
	}
	
	function unpublish(){
		$per_page 			= $this->input->get('per_page')?$this->input->get('per_page'):0;
		
		$user = $this->session->userdata('rsh_user');
		$data['where']['user_id'] 	= $user['user_id'];
		$data['where']['fstatus'] 	= 0;
		$data['select'] 	= 1;
		$data['page_size']  = USER_CENTER_PAGE_SIZE;
		$data['page_start'] = $per_page;
		$this->load->model('Borrow_model');
		$vdata = $this->get_user_info('borrow_unpublish','publish');
		
		$b_vdata = $this->Borrow_model->get_user_borrow($data);
		$vdata['borrow_list'] = $b_vdata['borrow_list'];
		//分页
		$this->load->library('pagination');
		$config['base_url'] = "/userborrow/unpublish/index.html?";
		$config['total_rows'] = $vdata['borrow_count'];
		$config['per_page'] = USER_CENTER_PAGE_SIZE; 
		$this->pagination->initialize($config); 
		$vdata['borrow_type'] 	= $this->Borrow_model->arrays('borrow_type');//标种
		$vdata['type'] = 2;
		$vdata['title'] 						= "尚未发布的借款".' - '.CON_KEYWORDS;
		$this->load->view('/user/user_loan_borrow', $vdata);
	}
	
	function repayment(){
		$per_page 			= $this->input->get('per_page')?$this->input->get('per_page'):0;
		
		$user = $this->session->userdata('rsh_user');
		$data['where']['user_id'] 	= $user['user_id'];
		$data['where']['fstatus'] 	= 0;
		$data['select'] 	= 1;
		$data['order']		= 'asc';
		$data['page_size']  = USER_CENTER_PAGE_SIZE;
		$data['page_start'] = (int)$per_page;
		$this->load->model('Borrow_model');
		$vdata = $this->get_user_info('borrow_repayment','repayment');
		
		$r_vdata = $this->Borrow_model->get_repayment_list($data);
		$vdata['repayment_list'] = $r_vdata['repayment_list'];
		
		if(count($vdata['repayment_list']) < 1){
			$data['where']['ftype'] 	= 4;
			$v1data = $this->Borrow_model->get_lz_repayment_list($data);
			if(count($v1data['repayment_list']) > 0){
				header("location:/userborrow/lzrepayment/index.html");exit;
			}
		}
		//分页
		$this->load->library('pagination');
		$config['base_url'] = "/userborrow/repayment/index.html?";
		$config['total_rows'] = $vdata['repayment_count'];
		$config['per_page'] = USER_CENTER_PAGE_SIZE; 
		$this->pagination->initialize($config); 
		$vdata['borrow_type'] 	= $this->Borrow_model->arrays('borrow_type');//标种
		$vdata['type'] = 3;
		$vdata['title'] 						= "正在还款的借款".' - '.CON_KEYWORDS;
		$this->load->view('/user/user_loan_repayment', $vdata);
	}
	
	/**
	 * 流转标还款列表
	 */
	function lzrepayment(){
		$per_page 			= $this->input->get('per_page')?$this->input->get('per_page'):0;
		
		$user = $this->session->userdata('rsh_user');
		$data['where']['user_id'] 	= $user['user_id'];
		$data['where']['fstatus'] 	= 0;
		$data['where']['ftype'] 	= 4;
		$data['select'] 	= 1;
		$data['order']		= '1';
		$data['page_size']  = USER_CENTER_PAGE_SIZE;
		$data['page_start'] = (int)$per_page;
		$this->load->model('Borrow_model');
		$vdata = $this->get_user_info('borrow_repayment','repayment');
		
		$lz_replayment = $this->Borrow_model->get_lz_repayment_list($data);
		$vdata['repayment_list'] = $lz_replayment['repayment_list'];
		//分页
		$this->load->library('pagination');
		$config['base_url'] = "/userborrow/lzrepayment/index.html?";
		$config['total_rows'] = $vdata['repayment_count'];
		$config['per_page'] = USER_CENTER_PAGE_SIZE; 
		$this->pagination->initialize($config); 
		$vdata['borrow_type'] 	= $this->Borrow_model->arrays('borrow_type');//标种
		$vdata['type'] = 5;
		$vdata['title'] 						= "正在还款的流转标".' - '.CON_KEYWORDS;
		$this->load->view('/user/user_loan_repayment', $vdata);
	}
	
	/**
	 * 已还完的借款标
	 */
	function over(){
		$per_page 			= $this->input->get('per_page')?$this->input->get('per_page'):0;
		
		$user = $this->session->userdata('rsh_user');
		$data['where']['user_id'] 	= $user['user_id'];
		$data['where']['fstatus'] 	= 3;
		$data['where']['fstatus1'] 	= 'over';
		$data['select'] 	= 1;
		$data['page_size']  = USER_CENTER_PAGE_SIZE;
		$data['page_start'] = (int)$per_page;
		$this->load->model('Borrow_model');
		$vdata = $this->get_user_info('borrow_over','over');
		
		$b_vdata = $this->Borrow_model->get_user_borrow($data);
		$vdata['borrow_list'] = $b_vdata['borrow_list'];
		
		//分页
		$this->load->library('pagination');
		$config['base_url'] = "/userborrow/over/index.html?";
		$config['total_rows'] = $vdata['borrow_count'];
		$config['per_page'] = USER_CENTER_PAGE_SIZE; 
		$this->pagination->initialize($config); 
		$vdata['borrow_type'] 	= $this->Borrow_model->arrays('borrow_type');//标种
		$vdata['type'] = 4;
		$vdata['title'] 						= "已还完的借款".' - '.CON_KEYWORDS;
		$this->load->view('/user/user_loan_borrow', $vdata);
	}
	
	function yjb(){
		$user = $this->session->userdata('rsh_user');
		$fstatus = intval($this->input->get('state'));
		$this->load->model('Yjb_model');
		$data['per_page'] = intval($this->input->get('per_page'));
		$data['page_size'] = 10;
		$data['fstatus']=$fstatus;
		$data['user_id']=$user['user_id'];
		if ($fstatus==0){
			$data['order'] = 'add_time';
		}elseif ($fstatus==1){
			$data['order'] = 'repay_time';
		}elseif ($fstatus==3){
			$data['order'] = 'add_time';
		}
		
		$tab_arr = array(0=>'yjb_success',1=>'yjb_coll',3=>'yjb_ycoll');
		$vdata = $this->get_user_info($tab_arr[$fstatus],'yjb','tender');
	
		
		$vdata_list = $this->Yjb_model->tender_list($data);
		$vdata['list'] = $vdata_list['list'];
		$vdata['total_money'] = $vdata_list['total_money'];
		$vdata['total_repay'] = $vdata_list['total_repay'];
		$vdata['total_people'] = $vdata_list['total_people'];
		
		$this->load->library('pagination');
		$config['base_url'] = '/userborrow/yjb.html?state='.$fstatus;
		$config['total_rows'] = $vdata_list['count'];
		$config['per_page'] = sys_admin_LIST_PAGE_SIZE; 
		$this->pagination->initialize($config); 
		$vdata['state'] = $fstatus;
		$vdata['title'] 						= "易居宝投资明细".' - '.CON_KEYWORDS;
		
		//var_dump($vdata);exit;
		$this->load->view('/user/yjb_list', $vdata);
	}
	
	function yjbborrow(){
		$user = $this->session->userdata('rsh_user');
		$this->load->model('Yjb_model');
		$data['where']['user_id'] = $user['user_id'];
		$data['per_page'] = intval($this->input->get('per_page'));
		$data['page_size'] = sys_admin_LIST_PAGE_SIZE;
		$ret = $this->Yjb_model->get_list($data);
		$this->load->library('pagination');
		$config['base_url'] = '/userborrow/yjb.html?pagesize='.$data['page_size'];
		$config['total_rows'] = $ret['count'];
		$config['per_page'] = $data['page_size']; 
		$this->pagination->initialize($config); 
		$vdata['list'] = $ret['list'];
		$vdata['type'] = 6;
		$vdata['title'] 						= "易居宝借款列表".' - '.CON_KEYWORDS;
		$this->load->view('/user/user_yjb_borrow', $vdata);
	}
	
	function yjbwithdraw($id){
		$id = intval($id);
		$user = $this->session->userdata('rsh_user');
		$this->load->model('Yjb_model');
		$obj = $this->Yjb_model->get_by_id($id);
		if ($obj['user_id']==$user['user_id'] && $obj['fstatus']<=1){
			$setArr = array('fstatus'=>5);
			$ret = $this->Yjb_model->update($setArr,array('id'=>$id));
			echo $ret ? 1:0;
			exit;
		}
		echo 0;
	}
	
	/**
	 * 可转让的债权
	 */
	function transfer(){
		$user = $this->session->userdata('rsh_user');
		$this->load->model('Creditor_model');
		$data['user_id'] = $user['user_id'];
		
		$vdata = $this->get_user_info('transfer','transferlog','tender');
		
		$list = $this->Creditor_model->get_mb_transfer_list($data);
		//var_dump($list);exit;
		$vdata['list'] = $list;
		$vdata['title'] = "可转让的债权".' - '.CON_KEYWORDS;
		$this->load->view('/user/user_borrow_transfer', $vdata);
	}
	
	/**
	 * 我发布的债权
	 */
	function mycreditor(){
		$user = $this->session->userdata('rsh_user');
		$this->load->model('Creditor_model');
		$data['user_id'] = $user['user_id'];
		$data['per_page'] = intval($this->input->get('per_page'));
		$data['page_size'] = 10;
		$data['order'] = 'fstatus';
		
		$vdata = $this->get_user_info('mycredit','transferlog','tender');
		
		$vdata_list = $this->Creditor_model->transfer_obj_list($data);
		$vdata['list'] = $vdata_list['list'];
		//var_dump($vdata);exit;
		//分页
		$this->load->library('pagination');
		$config['base_url'] = '/userborrow/mycreditor.html?size=10';
		$config['total_rows'] = $vdata['count'];
		$config['per_page'] = $data['page_size'];
		$this->pagination->initialize($config);
		
		$vdata['title'] = "我发布的债权".' - '.CON_KEYWORDS;
		$this->load->view('/user/user_borrow_mycreditor', $vdata);
	}
	
	/**
	 * 我购买的债权
	 */
	function transferlog(){
		$user = $this->session->userdata('rsh_user');
		$this->load->model('Creditor_model');
		$data['user_id'] = $user['user_id'];
		$data['per_page'] = intval($this->input->get('per_page'));
		$data['page_size'] = 10;
		
		$vdata = $this->get_user_info('mytran','transferlog','tender');
		
		$vdata_list = $this->Creditor_model->my_creditor_log($data);
		$vdata['list'] = $vdata_list['list'];
		//分页
		$this->load->library('pagination');
		$config['base_url'] = '/userborrow/transferlog.html?size=10';
		$config['total_rows'] = $vdata_list['count'];
		$config['per_page'] = $data['page_size']; 
		$this->pagination->initialize($config);
		$vdata['title'] = "我购买的债权".' - '.CON_KEYWORDS;
		$this->load->view('/user/user_borrow_transferlog', $vdata);
	}
	
	/**
	 * 我的待收债权明细
	 */
	function ctlog(){
		$user = $this->session->userdata('rsh_user');
		$data['per_page'] = intval($this->input->get('per_page'));
		$data['page_size'] = 10;
		$data['user_id'] = $user['user_id'];
		$this->load->model('Creditor_model');
		
		$vdata = $this->get_user_info('ctlog','transferlog','tender');
		
		$vdata_list = $this->Creditor_model->ctlog($data);
		$vdata['list'] = $vdata_list['list'];
		//分页
		$this->load->library('pagination');
		$config['base_url'] = '/userborrow/ctlog.html?size=10';
		$config['total_rows'] = $vdata['count'];
		$config['per_page'] = $data['page_size']; 
		$this->pagination->initialize($config);
		$vdata['title'] = "债权待收明细".' - '.CON_KEYWORDS;
		$vdata['type'] = 4;
		$this->load->view('/user/ctlog', $vdata);
	}
	
	/**
	 * 申请债权转让
	 */
	function dotransfer(){
		//echo '错误';
		$user = $this->session->userdata('rsh_user');
		$this->load->model('Creditor_model');
		$this->load->model('Mydb_model');
		$data['title'] = trim($this->input->post('title'));
		$data['tender_id'] = intval($this->input->post('tender_id'));
		$date = strtotime(date('Y-m-d'));
		//$sql = "select id from rsh_creditor_obj where user_id={$user['user_id']} and fstatus<4 and add_time>={$date} limit 1";
		$sql = "select p1.id from rsh_creditor_obj as p1 left join rsh_borrow_tender as p2 on p1.tender_id=p2.id 
				where p2.user_id={$user['user_id']} and p1.fstatus<4 and p1.add_time>={$date} limit 1";

		$row = $this->Mydb_model->query_sql($sql, 'row');
		if ($row){
			echo '一天只能发布一个债券转让';
			exit;
		}
		
		
		/*$sql = "SELECT p1.id FROM rsh_expmoney_deduct AS p1 LEFT JOIN rsh_borrow_collection AS p2 ON p1.collection_id=p2.id WHERE p2.tender_id={$data['tender_id']} LIMIT 1;";
		
		$row = $this->Mydb_model->query_sql($sql, 'row');
		if ($row){
			echo '该待收里含有体验金，不能转让';
			exit;
		}*/
		
		$sql = "select * from rsh_borrow_tender where id={$data['tender_id']}";
		$row = $this->Mydb_model->query_sql($sql, 'row');
		if (!$row ){
			echo '数据错误';
			exit;
		}
		if ($row['user_id'] != $user['user_id']){
			echo '数据错误';
			exit;
		}
		
		
		
		$ret = $this->Creditor_model->check_tender_id($data['tender_id']);
		if ($ret){
			echo '该债权已转让';
			exit;
		}
		if ($data['title']==''){
			echo '请选择标题';
			exit;
		}
		
		
		$time = date('Y-m-d');
    	$time = strtotime(date("Y-m-d",strtotime("$time -3 month")));
    	if ($row['add_time']>$time){
    		echo '未达到转让条件';
			exit;
    	}
    	
    	if ($row['account']<10000){
			echo '债权转让最低金额为10000';
			exit;
	    }
    	
		
		$data['fstatus'] = 1;
		$apr = floatval($this->input->post('apr'));
		$data['discount_apr'] = round($apr,2);
		if ($data['discount_apr']<0.2 || $data['discount_apr']>0.8){
			echo '折让率必须为0.2%-0.8%之间';
			exit;
		}
		
		$discount_account = $row['account']*(100-$data['discount_apr'])/100;
		$data['discount_account'] = round($discount_account,2);
		$data['discount_yes'] = 0;
		$data['content'] = $this->input->post('content',true);
		$data['add_time'] = time();
		$data['valid_time'] = intval($this->input->post('valid_time'));
		
		if ($data['content']==''){
			echo '请填写说明';
			exit;
		}
		if (!$data['valid_time']){
			echo '请选择有效期';
			exit;
		}
		//print_r($data);
		//exit;
		$ret = $this->Creditor_model->apply_transfer($data);
		if ($ret){
			echo '申请成功，请等待审核';
		}else{
			echo '操作失败，请联系客服';
		}
	}
	
	/**
	 * 取消申请
	 */
	function applycancel(){
		$user = $this->session->userdata('rsh_user');
		$id = intval($this->input->get('id'));
		$fstatus = 4;
		$this->load->model('Creditor_model');
		$obj = $this->Creditor_model->get_obj_by_id($id);
		if ($id>0 && $obj['fstatus']==1 && $user['user_id']==$obj['user_id']){
			$data = array(
				'id' => $id,
				'fstatus' => $fstatus,
				'verify_remark'=>'用户取消',
				'tender_id' => $obj['tender_id']
			);
			$ret = $this->Creditor_model->verify($data);
			echo $ret ? '操作成功':'操作失败';
		}else{
			echo '错误操作';
		}
	}
	
	
	/**
	 * 撤标
	 * @param $id
	 */
	function withdraw($id){
		$user = $this->session->userdata('rsh_user');
		$data['id'] 		= $id;
		$data['user_id'] 	= $user['user_id'];
		$data['ip'] 		= $this->input->ip_address();;
		$this->load->model('Borrow_model');
		echo $this->Borrow_model->Cancel($data);
	}
	
	/**
	 * 还款
	 * @param $id
	 */
	function repay($id){
		$user = $this->session->userdata('rsh_user');
		$data['repayment_id'] 	= $id;
		$data['user_id'] 		= $user['user_id'];
		$data['ip'] 			= $this->input->ip_address();
		$this->load->model('Borrow_model');
		echo $this->Borrow_model->repay($data);
	}
	
	/**
	 * 流转标还款
	 * @param $id
	 */
	function lz_repay($id){
		$user = $this->session->userdata('rsh_user');
		$data['collection_id'] 	= $id;
		$data['user_id'] 		= $user['user_id'];
		$data['ip'] 			= $this->input->ip_address();
		$this->load->model('Borrow_model');
		echo $this->Borrow_model->lz_repay($data);
	}
	
	/**
	 * 债权转让协议 html
	 * @param int $borrow_id
	 * @param int $tender_id
	 */
	function creditor_protocol($creditor_id,$user_id,$id){
		
		$creditor_id = intval($creditor_id);
		$this->load->model('Creditor_model');
		$this->load->model('Borrow_model');
		$this->load->model('User_model');
		
		//获取债权详情
		$creditor = $this->Creditor_model->get_obj_by_id($creditor_id);
		
		$borrow = $this->Borrow_model->get_borrow_obj($creditor['borrow_id'], array('select'=>1));
		
		$creditor_log = $this->Creditor_model->get_buyers_uid_by_id($creditor_id,$user_id,$id);
		
		$vdata['creditor_log'] = $creditor_log;
		
		$vdata['borrow'] 	= $borrow;
		
		$vdata['creditor'] 	= $creditor;
		$vdata['title'] 	= $creditor['title'].' - '.CON_KEYWORDS;
		
		$this->load->view('/borrow/borrow_creditor_protocol', $vdata);
	}
	
	
	/**
	 * 流转标协议书pdf
	 * @param int $borrow_id
	 * @param int $tender_id
	 */
	function creditorprotocol($creditor_id){
		$creditor_id = intval($creditor_id);
		if($creditor_id>0){
			$this->load->library('func');
			$user = $this->session->userdata('rsh_user');
			$this->load->model('Creditor_model');
			$clog = $this->Creditor_model->get_by_log_id($creditor_id);
			$html = $this->func->protocol_msg_creditor($clog['obj_id'],$user['user_id'],$creditor_id);
			$this->load->library('/MPDF56/mpdf');
			$this->mpdf->useAdobeCJK = true;
			$this->mpdf->SetAutoFont(AUTOFONT_ALL);
			$this->mpdf->SetDisplayMode('fullpage');
			$this->mpdf->showWatermarkText = true;
			$this->mpdf->SetAutoFont();
			
			$this->mpdf->showWatermarkText = true;
			$this->mpdf->SetAutoFont();
			
			$this->mpdf->WriteHTML($html);
			$this->mpdf->Output();
		}
	}
	
	
	
}
=======
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 用户表的列表信息
 * @author chenhuan
 *
 */
class Userborrow extends MY_Controller {
    
	function __construct(){
		parent::__construct();
		/** 登陆验证 */
		$user = $this->session->userdata('rsh_user');
		if(!isset($user['user_id'])){
			$str = $_SERVER['REQUEST_URI'];
			$this->js->go('/login.html');
		}elseif(!$user['user_id']){
			$this->js->go('/login.html');
		}
		
		$this->tab = 'user';
		$this->sub = 'borrow';
		
	}
	
	
	/**
	 * 成功投资的借款标
	 */
	function zhs_success(){
		$per_page 			= $this->input->get('per_page')?$this->input->get('per_page'):0;
		$user = $this->session->userdata('rsh_user');
		$data['where']['user_id'] 	= $user['user_id'];
		$data['where']['fstatus'] 	= 1;
		$data['page_size']  = USER_CENTER_PAGE_SIZE;
		$data['page_start'] = (int)$per_page;
		
		$this->load->model('Borrow_model');
		$vdata = $this->get_user_info('tender_success','success','tender');
		$tender_vdata= $this->Borrow_model->borrow_tender_user_list($data);

		//投标实际总金额
		$sj_account = 0;
		if( !empty($tender_vdata['tender_list']) ){
			foreach( $tender_vdata['tender_list'] as $k => $v ){
				$sj_account += $v['account'];
				if($v['account'] <= $v['account_yes']) $v['fstatus'] = 3;
			}
		}
		$vdata['tender_money_count'] = $sj_account;
		$vdata['tender_list'] = $tender_vdata['tender_list'];//成功投资的列表
		//分页
		$this->load->library('pagination');
		$config['base_url'] 	= "/userborrow/success.html?";
		$config['total_rows'] 	= $vdata['tender_count'];
		$config['per_page'] 	= USER_CENTER_PAGE_SIZE; 
		$this->pagination->initialize($config); 
		
		$vdata['type'] = 1;
		$vdata['title'] = "成功投资的借款标".' - '.CON_KEYWORDS; 
		$this->load->view('/user/zhs_user_tender_success', $vdata);
		
	}
	
	
	/**
	 * 待收明细
	 */
	function zhs_coll(){
		$act = $this->input->get('act');
		if ($act == 'excel'){
			$this->collexcel();
		}else{
			$per_page 					= $this->input->get('per_page')?$this->input->get('per_page'):0;
			$user = $this->session->userdata('rsh_user');
			
			$data['where']['user_id'] 	= $user['user_id'];
			$data['where']['fstatus'] 	= 0;
			$data['where']['strtime'] 	= $this->input->get('strtime') ? $this->input->get('strtime') : '';
			$data['where']['endtime'] 	= $this->input->get('endtime') ? $this->input->get('endtime') : '';
			$data['select'] 	= 1;
			$data['order'] 		= 1;
			$data['page_size']  = USER_CENTER_PAGE_SIZE;
			$data['page_start'] = (int)$per_page;
			$this->load->model('Borrow_model');
		
			$vdata = $this->get_user_info('tender_coll','success','tender');
			$data['type'] = 3;
			$coll_vdata = $this->Borrow_model->get_user_collection($data);
			$vdata['collection_list'] = $coll_vdata['collection_list'];
			//if(count($vdata['collection_list']) < 1)$this->js->alert('查询无符合条件的数据');
			//分页
			$this->load->library('pagination');
			$config['base_url'] = "/userborrow/zhs_coll.html?strtime=".$data['where']['strtime']."&endtime=".$data['where']['endtime'];
			$config['total_rows'] = $vdata['collection_count'];
			$config['per_page'] = USER_CENTER_PAGE_SIZE; 
			$this->pagination->initialize($config); 
			$vdata['type'] = $data['type'];
			$vdata['title'] 						= "未收款明细".' - '.CON_KEYWORDS;
			$vdata['sub'] = 'tender';
			$vdata['user_sub'] ='tender_colldet';
			$vdata['where']['strtime'] 	= $this->input->get('strtime') ? $this->input->get('strtime') : '';
			$vdata['where']['endtime'] 	= $this->input->get('endtime') ? $this->input->get('endtime') : '';
			$vdata['sub_1'] = "会员中心";
			$vdata['sub_1_href'] = "/user/zhs_center.html";
			$vdata['sub_2'] = "待收回款";
			$vdata['sub_2_href'] = "/userborrow/zhs_coll.html";
			$vdata['borrow_type'] 				= $this->Borrow_model->arrays('borrow_type');//标类型
			$this->load->view('/user/zhs_user_borrow_ycolldetail', $vdata);
		}
	}
	
	function collexcel(){
		$user = $this->session->userdata('rsh_user');
		$data['where']['user_id'] 	= $user['user_id'];
		$data['where']['fstatus'] 	= 0;
		$data['where']['strtime'] 	= $this->input->get('strtime') ? $this->input->get('strtime') : '';
		$data['where']['endtime'] 	= $this->input->get('endtime') ? $this->input->get('endtime') : '';
		$data['select'] 	= 1;
		$data['order'] 		= 1;
		$data['limit'] = 'all';
		$this->load->model('Borrow_model');
		$list = $this->Borrow_model->get_user_collection($data);
		
		$this->load->library('PHPExcel');
		$this->load->library('PHPExcel/IOFactory'); 
		$objPHPExcel = new PHPExcel();
		$objPHPExcel->getProperties()->setTitle("待收明细")->setDescription(COMPANY_NAME); 
		$objPHPExcel->setActiveSheetIndex(0);
		$titles = array("借款标", "期数", "应收时间", "应收本金", "应收利息");
		
		for($i = 0; $i < count($titles); $i ++){
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($i, 1, $titles[$i]);
		}
		
		for($i = 0; $i < count($list); $i++){
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(0, $i+2, $list[$i]['title']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(1, $i+2, $list[$i]['time_limit']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(2, $i+2, date('Y-m-d',$list[$i]['repay_time']));
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(3, $i+2, $list[$i]['capital']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(4, $i+2, $list[$i]['interest']);
		}
		
		$objWriter = IOFactory::createWriter($objPHPExcel, 'Excel5'); 
		//发送标题强制用户下载文件 
		header('Content-Type: application/vnd.ms-excel'); 
		header('Content-Disposition: attachment;filename="待收明细.xls"'); 
		header('Cache-Control: max-age=0'); 
		$objWriter->save('php://output'); 
	}
	
	
	/**
	 * 已收明细
	 */
	function zhs_ycoll(){
		$per_page 			= $this->input->get('per_page')?$this->input->get('per_page'):0;
		
		$user = $this->session->userdata('rsh_user');
		$data['where']['strtime'] 	= $this->input->get('strtime') ? $this->input->get('strtime') : '';
		$data['where']['endtime'] 	= $this->input->get('endtime') ? $this->input->get('endtime') : '';
		$data['where']['user_id'] 	= $user['user_id'];
		$data['where']['fstatus'] 	= 1;
		$data['select'] 	= 1;
		$data['order'] 		= 4;
		$data['page_size']  = USER_CENTER_PAGE_SIZE;
		$data['page_start'] = (int)$per_page;
		$this->load->model('Borrow_model');
		$vdata = $this->get_user_info('tender_ycoll','success','tender');
		
		$data['type'] = 4;
		$ycoll_vdata = $this->Borrow_model->get_user_collection($data);
		$vdata['collection_list'] = $ycoll_vdata['collection_list'];
		//if(count($vdata['collection_list']) < 1)$this->js->alert('查询无符合条件的数据');
		//分页
		$this->load->library('pagination');
		$config['base_url'] = "/userborrow/zhs_ycoll.html?";
		$config['total_rows'] = $vdata['collection_count'];
		$config['per_page'] = USER_CENTER_PAGE_SIZE; 
		$this->pagination->initialize($config); 
		$vdata['where']['strtime'] 	= $this->input->get('strtime') ? $this->input->get('strtime') : '';
		$vdata['where']['endtime'] 	= $this->input->get('endtime') ? $this->input->get('endtime') : '';
		$vdata['type'] = $data['type'];
		$vdata['sub'] = 'tender';
		$vdata['user_sub'] ='tender_colldet';
		$vdata['title'] 						= "已收明细".' - '.CON_KEYWORDS;
		$vdata['sub_1'] = "会员中心";
		$vdata['sub_1_href'] = "/user/zhs_center.html";
		$vdata['sub_2'] = "已收回款";
		$vdata['sub_2_href'] = "/userborrow/zhs_ycoll.html";
		$vdata['borrow_type'] 				= $this->Borrow_model->arrays('borrow_type');//标类型
		$this->load->view('/user/zhs_user_borrow_ycolldetail', $vdata);
	}
	
	//******************************
	function zsh_ycoll(){//投资
		$per_page 			= $this->input->get('per_page')?$this->input->get('per_page'):0;
		
		$user = $this->session->userdata('rsh_user');
		$data['where']['user_id'] 	= $user['user_id'];
		$data['where']['fstatus'] 	= 1;
		$data['select'] 	= 1;
		$data['order'] 		= 4;
		$data['page_size']  = USER_CENTER_PAGE_SIZE;
		$data['page_start'] = (int)$per_page;
		$this->load->model('Borrow_model');
		$vdata = $this->get_user_info('tender_ycoll','success','tender');
		
		$ycoll_vdata = $this->Borrow_model->get_user_collection($data);
		$vdata['collection_list'] = $ycoll_vdata['collection_list'];
		
		//分页
		$this->load->library('pagination');
		$config['base_url'] = "/userborrow/ycoll.html?";
		$config['total_rows'] = $vdata['collection_count'];
		$config['per_page'] = USER_CENTER_PAGE_SIZE; 
		$this->pagination->initialize($config); 
		$vdata['type'] = 4;
		$vdata['title'] 						= "投资赎回".' - '.CON_KEYWORDS;
		$vdata['user_sub'] 						= "tender_coll";
		$vdata['sub'] 						= "tender";
		$this->load->view('/user/zhs_user_borrow_ycoll', $vdata);
	}
	
	function zsh_ycoll_detail(){//回款投资：包括未回款、已回款
		$per_page 			= $this->input->get('per_page')?$this->input->get('per_page'):0;
		$user = $this->session->userdata('rsh_user');
		$data['where']['user_id'] 	= $user['user_id'];
		$data['where']['fstatus'] 	= 1;
		$data['select'] 	= 1;
		$data['order'] 		= 4;
		$data['page_size']  = USER_CENTER_PAGE_SIZE;
		$data['page_start'] = (int)$per_page;
		$this->load->model('Borrow_model');
		$vdata = $this->get_user_info('tender_ycoll','success','tender');
		
		$ycoll_vdata = $this->Borrow_model->get_user_collection($data);
		$vdata['collection_list'] = $ycoll_vdata['collection_list'];
		
		//分页
		$this->load->library('pagination');
		$config['base_url'] = "/userborrow/ycoll.html?";
		$config['total_rows'] = $vdata['collection_count'];
		$config['per_page'] = USER_CENTER_PAGE_SIZE; 
		$this->pagination->initialize($config); 
		$vdata['type'] = 4;
		$vdata['title'] 						= "回款明细".' - '.CON_KEYWORDS;
		$vdata['user_sub'] 						= "tender_colldet";
		$vdata['sub'] 						= "tender";
		$this->load->view('/user/zhs_user_borrow_ycolldetail', $vdata);
	}
	

	/**
	 * 正在投资的借款标
	 */
	function now(){
		$per_page 			= $this->input->get('per_page')?$this->input->get('per_page'):0;
		
		$user = $this->session->userdata('rsh_user');
		$data['where']['user_id'] 	= $user['user_id'];
		$data['where']['fstatus'] 	= 1;
		$data['select'] 			= 1;
		$data['page_size']  		= USER_CENTER_PAGE_SIZE;
		$data['page_start'] 		= (int)$per_page;
		$this->load->model('Borrow_model');
		
		$vdata = $this->get_user_info('tender_now','success','tender');
		
		$now_vdata = $this->Borrow_model->get_borrow_tender_lists($data);
		$vdata['tender_list'] = $now_vdata['tender_list'];
		
		//分页
		$this->load->library('pagination');
		$config['base_url'] = "/userborrow/now/index.html?";
		$config['total_rows'] = $vdata['tender_count'];
		$config['per_page'] = USER_CENTER_PAGE_SIZE; 
		$this->pagination->initialize($config); 
		$vdata['type'] = 2;
		$vdata['title'] 						= "正在投标的借款".' - '.CON_KEYWORDS;
		$this->load->view('/user/user_borrow_now', $vdata);
	}
	
	/**
	 * 正在投资的借款标
	 */
	function zhs_now(){
		$per_page 			= $this->input->get('per_page')?$this->input->get('per_page'):0;
		
		$user = $this->session->userdata('rsh_user');
		$data['where']['user_id'] 	= $user['user_id'];
		$data['where']['fstatus'] 	= 1;
		$data['select'] 			= 1;
		$data['page_size']  		= USER_CENTER_PAGE_SIZE;
		$data['page_start'] 		= (int)$per_page;
		$this->load->model('Borrow_model');
		
		$vdata = $this->get_user_info('tender_now','success','tender');
		
		$now_vdata = $this->Borrow_model->get_borrow_tender_lists($data);
		$vdata['tender_list'] = $now_vdata['tender_list'];
		
		//分页
		$this->load->library('pagination');
		$config['base_url'] = "/userborrow/now/index.html?";
		$config['total_rows'] = $vdata['tender_count'];
		$config['per_page'] = USER_CENTER_PAGE_SIZE; 
		$this->pagination->initialize($config); 
		$vdata['type'] = 2;
		$vdata['title'] 						= "正在投标的借款".' - '.CON_KEYWORDS;
		$this->load->view('/user/zhs_user_borrow_now', $vdata);
	}
	
	function publish(){
		$per_page 			= $this->input->get('per_page')?$this->input->get('per_page'):0;
		
		$user = $this->session->userdata('rsh_user');
		$data['where']['user_id'] 	= $user['user_id'];
		$data['where']['fstatus'] 	= 1;
		$data['select'] 	= 1;
		$data['page_size']  = USER_CENTER_PAGE_SIZE;
		$data['page_start'] = (int)$per_page;
		$this->load->model('Borrow_model');
		$vdata = $this->get_user_info('borrow_publish','publish');
		
		$borrow_vdata = $this->Borrow_model->get_user_borrow($data);
		$vdata['borrow_list'] = $borrow_vdata['borrow_list'];
		
		//分页
		$this->load->library('pagination');
		$config['base_url'] = "/userborrow/publish/index.html?";
		$config['total_rows'] = $vdata['borrow_count'];
		$config['per_page'] = USER_CENTER_PAGE_SIZE; 
		$this->pagination->initialize($config); 
		$vdata['borrow_type'] 	= $this->Borrow_model->arrays('borrow_type');//标种
		$vdata['type'] = 1;
		$vdata['title'] 						= "正在招标的借款".' - '.CON_KEYWORDS;
		$this->load->view('/user/user_loan_borrow', $vdata);
	}
	
	function unpublish(){
		$per_page 			= $this->input->get('per_page')?$this->input->get('per_page'):0;
		
		$user = $this->session->userdata('rsh_user');
		$data['where']['user_id'] 	= $user['user_id'];
		$data['where']['fstatus'] 	= 0;
		$data['select'] 	= 1;
		$data['page_size']  = USER_CENTER_PAGE_SIZE;
		$data['page_start'] = $per_page;
		$this->load->model('Borrow_model');
		$vdata = $this->get_user_info('borrow_unpublish','publish');
		
		$b_vdata = $this->Borrow_model->get_user_borrow($data);
		$vdata['borrow_list'] = $b_vdata['borrow_list'];
		//分页
		$this->load->library('pagination');
		$config['base_url'] = "/userborrow/unpublish/index.html?";
		$config['total_rows'] = $vdata['borrow_count'];
		$config['per_page'] = USER_CENTER_PAGE_SIZE; 
		$this->pagination->initialize($config); 
		$vdata['borrow_type'] 	= $this->Borrow_model->arrays('borrow_type');//标种
		$vdata['type'] = 2;
		$vdata['title'] 						= "尚未发布的借款".' - '.CON_KEYWORDS;
		$this->load->view('/user/user_loan_borrow', $vdata);
	}
	
	function repayment(){
		$per_page 			= $this->input->get('per_page')?$this->input->get('per_page'):0;
		
		$user = $this->session->userdata('rsh_user');
		$data['where']['user_id'] 	= $user['user_id'];
		$data['where']['fstatus'] 	= 0;
		$data['select'] 	= 1;
		$data['order']		= 'asc';
		$data['page_size']  = USER_CENTER_PAGE_SIZE;
		$data['page_start'] = (int)$per_page;
		$this->load->model('Borrow_model');
		$vdata = $this->get_user_info('borrow_repayment','repayment');
		
		$r_vdata = $this->Borrow_model->get_repayment_list($data);
		$vdata['repayment_list'] = $r_vdata['repayment_list'];
		
		if(count($vdata['repayment_list']) < 1){
			$data['where']['ftype'] 	= 4;
			$v1data = $this->Borrow_model->get_lz_repayment_list($data);
			if(count($v1data['repayment_list']) > 0){
				header("location:/userborrow/lzrepayment/index.html");exit;
			}
		}
		//分页
		$this->load->library('pagination');
		$config['base_url'] = "/userborrow/repayment/index.html?";
		$config['total_rows'] = $vdata['repayment_count'];
		$config['per_page'] = USER_CENTER_PAGE_SIZE; 
		$this->pagination->initialize($config); 
		$vdata['borrow_type'] 	= $this->Borrow_model->arrays('borrow_type');//标种
		$vdata['type'] = 3;
		$vdata['title'] 						= "正在还款的借款".' - '.CON_KEYWORDS;
		$this->load->view('/user/user_loan_repayment', $vdata);
	}
	
	/**
	 * 流转标还款列表
	 */
	function lzrepayment(){
		$per_page 			= $this->input->get('per_page')?$this->input->get('per_page'):0;
		
		$user = $this->session->userdata('rsh_user');
		$data['where']['user_id'] 	= $user['user_id'];
		$data['where']['fstatus'] 	= 0;
		$data['where']['ftype'] 	= 4;
		$data['select'] 	= 1;
		$data['order']		= '1';
		$data['page_size']  = USER_CENTER_PAGE_SIZE;
		$data['page_start'] = (int)$per_page;
		$this->load->model('Borrow_model');
		$vdata = $this->get_user_info('borrow_repayment','repayment');
		
		$lz_replayment = $this->Borrow_model->get_lz_repayment_list($data);
		$vdata['repayment_list'] = $lz_replayment['repayment_list'];
		//分页
		$this->load->library('pagination');
		$config['base_url'] = "/userborrow/lzrepayment/index.html?";
		$config['total_rows'] = $vdata['repayment_count'];
		$config['per_page'] = USER_CENTER_PAGE_SIZE; 
		$this->pagination->initialize($config); 
		$vdata['borrow_type'] 	= $this->Borrow_model->arrays('borrow_type');//标种
		$vdata['type'] = 5;
		$vdata['title'] 						= "正在还款的流转标".' - '.CON_KEYWORDS;
		$this->load->view('/user/user_loan_repayment', $vdata);
	}
	
	/**
	 * 已还完的借款标
	 */
	function over(){
		$per_page 			= $this->input->get('per_page')?$this->input->get('per_page'):0;
		
		$user = $this->session->userdata('rsh_user');
		$data['where']['user_id'] 	= $user['user_id'];
		$data['where']['fstatus'] 	= 3;
		$data['where']['fstatus1'] 	= 'over';
		$data['select'] 	= 1;
		$data['page_size']  = USER_CENTER_PAGE_SIZE;
		$data['page_start'] = (int)$per_page;
		$this->load->model('Borrow_model');
		$vdata = $this->get_user_info('borrow_over','over');
		
		$b_vdata = $this->Borrow_model->get_user_borrow($data);
		$vdata['borrow_list'] = $b_vdata['borrow_list'];
		
		//分页
		$this->load->library('pagination');
		$config['base_url'] = "/userborrow/over/index.html?";
		$config['total_rows'] = $vdata['borrow_count'];
		$config['per_page'] = USER_CENTER_PAGE_SIZE; 
		$this->pagination->initialize($config); 
		$vdata['borrow_type'] 	= $this->Borrow_model->arrays('borrow_type');//标种
		$vdata['type'] = 4;
		$vdata['title'] 						= "已还完的借款".' - '.CON_KEYWORDS;
		$this->load->view('/user/user_loan_borrow', $vdata);
	}
	
	function yjb(){
		$user = $this->session->userdata('rsh_user');
		$fstatus = intval($this->input->get('state'));
		$this->load->model('Yjb_model');
		$data['per_page'] = intval($this->input->get('per_page'));
		$data['page_size'] = 10;
		$data['fstatus']=$fstatus;
		$data['user_id']=$user['user_id'];
		if ($fstatus==0){
			$data['order'] = 'add_time';
		}elseif ($fstatus==1){
			$data['order'] = 'repay_time';
		}elseif ($fstatus==3){
			$data['order'] = 'add_time';
		}
		
		$tab_arr = array(0=>'yjb_success',1=>'yjb_coll',3=>'yjb_ycoll');
		$vdata = $this->get_user_info($tab_arr[$fstatus],'yjb','tender');
	
		
		$vdata_list = $this->Yjb_model->tender_list($data);
		$vdata['list'] = $vdata_list['list'];
		$vdata['total_money'] = $vdata_list['total_money'];
		$vdata['total_repay'] = $vdata_list['total_repay'];
		$vdata['total_people'] = $vdata_list['total_people'];
		
		$this->load->library('pagination');
		$config['base_url'] = '/userborrow/yjb.html?state='.$fstatus;
		$config['total_rows'] = $vdata_list['count'];
		$config['per_page'] = sys_admin_LIST_PAGE_SIZE; 
		$this->pagination->initialize($config); 
		$vdata['state'] = $fstatus;
		$vdata['title'] 						= "易居宝投资明细".' - '.CON_KEYWORDS;
		
		//var_dump($vdata);exit;
		$this->load->view('/user/yjb_list', $vdata);
	}
	
	function yjbborrow(){
		$user = $this->session->userdata('rsh_user');
		$this->load->model('Yjb_model');
		$data['where']['user_id'] = $user['user_id'];
		$data['per_page'] = intval($this->input->get('per_page'));
		$data['page_size'] = sys_admin_LIST_PAGE_SIZE;
		$ret = $this->Yjb_model->get_list($data);
		$this->load->library('pagination');
		$config['base_url'] = '/userborrow/yjb.html?pagesize='.$data['page_size'];
		$config['total_rows'] = $ret['count'];
		$config['per_page'] = $data['page_size']; 
		$this->pagination->initialize($config); 
		$vdata['list'] = $ret['list'];
		$vdata['type'] = 6;
		$vdata['title'] 						= "易居宝借款列表".' - '.CON_KEYWORDS;
		$this->load->view('/user/user_yjb_borrow', $vdata);
	}
	
	function yjbwithdraw($id){
		$id = intval($id);
		$user = $this->session->userdata('rsh_user');
		$this->load->model('Yjb_model');
		$obj = $this->Yjb_model->get_by_id($id);
		if ($obj['user_id']==$user['user_id'] && $obj['fstatus']<=1){
			$setArr = array('fstatus'=>5);
			$ret = $this->Yjb_model->update($setArr,array('id'=>$id));
			echo $ret ? 1:0;
			exit;
		}
		echo 0;
	}
	
	/**
	 * 可转让的债权
	 */
	function transfer(){
		$user = $this->session->userdata('rsh_user');
		$this->load->model('Creditor_model');
		$data['user_id'] = $user['user_id'];
		
		$vdata = $this->get_user_info('transfer','transferlog','tender');
		
		$list = $this->Creditor_model->get_mb_transfer_list($data);
		//var_dump($list);exit;
		$vdata['list'] = $list;
		$vdata['title'] = "可转让的债权".' - '.CON_KEYWORDS;
		$this->load->view('/user/user_borrow_transfer', $vdata);
	}
	
	/**
	 * 我发布的债权
	 */
	function mycreditor(){
		$user = $this->session->userdata('rsh_user');
		$this->load->model('Creditor_model');
		$data['user_id'] = $user['user_id'];
		$data['per_page'] = intval($this->input->get('per_page'));
		$data['page_size'] = 10;
		$data['order'] = 'fstatus';
		
		$vdata = $this->get_user_info('mycredit','transferlog','tender');
		
		$vdata_list = $this->Creditor_model->transfer_obj_list($data);
		$vdata['list'] = $vdata_list['list'];
		//var_dump($vdata);exit;
		//分页
		$this->load->library('pagination');
		$config['base_url'] = '/userborrow/mycreditor.html?size=10';
		$config['total_rows'] = $vdata['count'];
		$config['per_page'] = $data['page_size'];
		$this->pagination->initialize($config);
		
		$vdata['title'] = "我发布的债权".' - '.CON_KEYWORDS;
		$this->load->view('/user/user_borrow_mycreditor', $vdata);
	}
	
	/**
	 * 我购买的债权
	 */
	function transferlog(){
		$user = $this->session->userdata('rsh_user');
		$this->load->model('Creditor_model');
		$data['user_id'] = $user['user_id'];
		$data['per_page'] = intval($this->input->get('per_page'));
		$data['page_size'] = 10;
		
		$vdata = $this->get_user_info('mytran','transferlog','tender');
		
		$vdata_list = $this->Creditor_model->my_creditor_log($data);
		$vdata['list'] = $vdata_list['list'];
		//分页
		$this->load->library('pagination');
		$config['base_url'] = '/userborrow/transferlog.html?size=10';
		$config['total_rows'] = $vdata_list['count'];
		$config['per_page'] = $data['page_size']; 
		$this->pagination->initialize($config);
		$vdata['title'] = "我购买的债权".' - '.CON_KEYWORDS;
		$this->load->view('/user/user_borrow_transferlog', $vdata);
	}
	
	/**
	 * 我的待收债权明细
	 */
	function ctlog(){
		$user = $this->session->userdata('rsh_user');
		$data['per_page'] = intval($this->input->get('per_page'));
		$data['page_size'] = 10;
		$data['user_id'] = $user['user_id'];
		$this->load->model('Creditor_model');
		
		$vdata = $this->get_user_info('ctlog','transferlog','tender');
		
		$vdata_list = $this->Creditor_model->ctlog($data);
		$vdata['list'] = $vdata_list['list'];
		//分页
		$this->load->library('pagination');
		$config['base_url'] = '/userborrow/ctlog.html?size=10';
		$config['total_rows'] = $vdata['count'];
		$config['per_page'] = $data['page_size']; 
		$this->pagination->initialize($config);
		$vdata['title'] = "债权待收明细".' - '.CON_KEYWORDS;
		$vdata['type'] = 4;
		$this->load->view('/user/ctlog', $vdata);
	}
	
	/**
	 * 申请债权转让
	 */
	function dotransfer(){
		//echo '错误';
		$user = $this->session->userdata('rsh_user');
		$this->load->model('Creditor_model');
		$this->load->model('Mydb_model');
		$data['title'] = trim($this->input->post('title'));
		$data['tender_id'] = intval($this->input->post('tender_id'));
		$date = strtotime(date('Y-m-d'));
		//$sql = "select id from rsh_creditor_obj where user_id={$user['user_id']} and fstatus<4 and add_time>={$date} limit 1";
		$sql = "select p1.id from rsh_creditor_obj as p1 left join rsh_borrow_tender as p2 on p1.tender_id=p2.id 
				where p2.user_id={$user['user_id']} and p1.fstatus<4 and p1.add_time>={$date} limit 1";

		$row = $this->Mydb_model->query_sql($sql, 'row');
		if ($row){
			echo '一天只能发布一个债券转让';
			exit;
		}
		
		
		/*$sql = "SELECT p1.id FROM rsh_expmoney_deduct AS p1 LEFT JOIN rsh_borrow_collection AS p2 ON p1.collection_id=p2.id WHERE p2.tender_id={$data['tender_id']} LIMIT 1;";
		
		$row = $this->Mydb_model->query_sql($sql, 'row');
		if ($row){
			echo '该待收里含有体验金，不能转让';
			exit;
		}*/
		
		$sql = "select * from rsh_borrow_tender where id={$data['tender_id']}";
		$row = $this->Mydb_model->query_sql($sql, 'row');
		if (!$row ){
			echo '数据错误';
			exit;
		}
		if ($row['user_id'] != $user['user_id']){
			echo '数据错误';
			exit;
		}
		
		
		
		$ret = $this->Creditor_model->check_tender_id($data['tender_id']);
		if ($ret){
			echo '该债权已转让';
			exit;
		}
		if ($data['title']==''){
			echo '请选择标题';
			exit;
		}
		
		
		$time = date('Y-m-d');
    	$time = strtotime(date("Y-m-d",strtotime("$time -3 month")));
    	if ($row['add_time']>$time){
    		echo '未达到转让条件';
			exit;
    	}
    	
    	if ($row['account']<10000){
			echo '债权转让最低金额为10000';
			exit;
	    }
    	
		
		$data['fstatus'] = 1;
		$apr = floatval($this->input->post('apr'));
		$data['discount_apr'] = round($apr,2);
		if ($data['discount_apr']<0.2 || $data['discount_apr']>0.8){
			echo '折让率必须为0.2%-0.8%之间';
			exit;
		}
		
		$discount_account = $row['account']*(100-$data['discount_apr'])/100;
		$data['discount_account'] = round($discount_account,2);
		$data['discount_yes'] = 0;
		$data['content'] = $this->input->post('content',true);
		$data['add_time'] = time();
		$data['valid_time'] = intval($this->input->post('valid_time'));
		
		if ($data['content']==''){
			echo '请填写说明';
			exit;
		}
		if (!$data['valid_time']){
			echo '请选择有效期';
			exit;
		}
		//print_r($data);
		//exit;
		$ret = $this->Creditor_model->apply_transfer($data);
		if ($ret){
			echo '申请成功，请等待审核';
		}else{
			echo '操作失败，请联系客服';
		}
	}
	
	/**
	 * 取消申请
	 */
	function applycancel(){
		$user = $this->session->userdata('rsh_user');
		$id = intval($this->input->get('id'));
		$fstatus = 4;
		$this->load->model('Creditor_model');
		$obj = $this->Creditor_model->get_obj_by_id($id);
		if ($id>0 && $obj['fstatus']==1 && $user['user_id']==$obj['user_id']){
			$data = array(
				'id' => $id,
				'fstatus' => $fstatus,
				'verify_remark'=>'用户取消',
				'tender_id' => $obj['tender_id']
			);
			$ret = $this->Creditor_model->verify($data);
			echo $ret ? '操作成功':'操作失败';
		}else{
			echo '错误操作';
		}
	}
	
	
	/**
	 * 撤标
	 * @param $id
	 */
	function withdraw($id){
		$user = $this->session->userdata('rsh_user');
		$data['id'] 		= $id;
		$data['user_id'] 	= $user['user_id'];
		$data['ip'] 		= $this->input->ip_address();;
		$this->load->model('Borrow_model');
		echo $this->Borrow_model->Cancel($data);
	}
	
	/**
	 * 还款
	 * @param $id
	 */
	function repay($id){
		$user = $this->session->userdata('rsh_user');
		$data['repayment_id'] 	= $id;
		$data['user_id'] 		= $user['user_id'];
		$data['ip'] 			= $this->input->ip_address();
		$this->load->model('Borrow_model');
		echo $this->Borrow_model->repay($data);
	}
	
	/**
	 * 流转标还款
	 * @param $id
	 */
	function lz_repay($id){
		$user = $this->session->userdata('rsh_user');
		$data['collection_id'] 	= $id;
		$data['user_id'] 		= $user['user_id'];
		$data['ip'] 			= $this->input->ip_address();
		$this->load->model('Borrow_model');
		echo $this->Borrow_model->lz_repay($data);
	}
	
	/**
	 * 债权转让协议 html
	 * @param int $borrow_id
	 * @param int $tender_id
	 */
	function creditor_protocol($creditor_id,$user_id,$id){
		
		$creditor_id = intval($creditor_id);
		$this->load->model('Creditor_model');
		$this->load->model('Borrow_model');
		$this->load->model('User_model');
		
		//获取债权详情
		$creditor = $this->Creditor_model->get_obj_by_id($creditor_id);
		
		$borrow = $this->Borrow_model->get_borrow_obj($creditor['borrow_id'], array('select'=>1));
		
		$creditor_log = $this->Creditor_model->get_buyers_uid_by_id($creditor_id,$user_id,$id);
		
		$vdata['creditor_log'] = $creditor_log;
		
		$vdata['borrow'] 	= $borrow;
		
		$vdata['creditor'] 	= $creditor;
		$vdata['title'] 	= $creditor['title'].' - '.CON_KEYWORDS;
		
		$this->load->view('/borrow/borrow_creditor_protocol', $vdata);
	}
	
	
	/**
	 * 流转标协议书pdf
	 * @param int $borrow_id
	 * @param int $tender_id
	 */
	function creditorprotocol($creditor_id){
		$creditor_id = intval($creditor_id);
		if($creditor_id>0){
			$this->load->library('func');
			$user = $this->session->userdata('rsh_user');
			$this->load->model('Creditor_model');
			$clog = $this->Creditor_model->get_by_log_id($creditor_id);
			$html = $this->func->protocol_msg_creditor($clog['obj_id'],$user['user_id'],$creditor_id);
			$this->load->library('/MPDF56/mpdf');
			$this->mpdf->useAdobeCJK = true;
			$this->mpdf->SetAutoFont(AUTOFONT_ALL);
			$this->mpdf->SetDisplayMode('fullpage');
			$this->mpdf->showWatermarkText = true;
			$this->mpdf->SetAutoFont();
			
			$this->mpdf->showWatermarkText = true;
			$this->mpdf->SetAutoFont();
			
			$this->mpdf->WriteHTML($html);
			$this->mpdf->Output();
		}
	}
	
	
	
}
>>>>>>> d47623b610d79a4eeb489500830c00d70933f414
